SET ANSI_NULLS ON
SET ANSI_WARNINGS ON
GO

IF EXISTS (SELECT 1 FROM sys.Objects WHERE  OBJECT_ID = OBJECT_ID(N'[MD3].[LogErrorParse]'))
BEGIN				
	DROP PROCEDURE [MD3].LogErrorParse
END
GO
CREATE PROCEDURE [MD3].LogErrorParse
	(
		@ErrorNumber INT = NULL OUTPUT ,
		@ErrorMessage NVARCHAR(MAX) = NULL OUTPUT ,
		@ErrorLine		INT = NULL OUTPUT ,
		@ErrorState		INT = NULL OUTPUT ,
		@ErrorSeverity	INT = NULL OUTPUT 
	)
AS
BEGIN 

	--Parses the last error logged to MD3.Log, if it is the last row.  If the last row is not an E then we have some kind of thread-safety problem, which should not exist.  
	SET NOCOUNT ON

	SELECT 
		@ErrorNumber = ErrorNumber,
		@ErrorMessage = ErrorMessage ,
		@ErrorLine		=ErrorLine,
		@ErrorState		= ErrorState	,
		@ErrorSeverity	= ErrorSeverity
	FROM MD3.Log
	WHERE Severity = 'E' 
	AND ID = (SELECT MAX(ID) FROM MD3.Log)
	


	IF @ErrorNumber IS NULL
	BEGIN
		RAISERROR ('ASSERT FAILED: The last entry in MD3.Log was not an error.',16,1)
		RETURN 1
	END

END
GO